import random

import werkzeug
from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for, abort, Response
from models.my_model import Post, User, R, UserRo, Comment, Like
from flask_login import current_user

page_profile = Blueprint("profile", __name__)


@page_profile.route("/user/profile/<int:user_id>")
def user_profile(user_id):
    cu = User()
    cu.id = 0
    cu.like = 0
    if current_user.is_authenticated:
        cu = current_user
        like: Like = Like.query.filter(Like.user_id == current_user.id, Like.target_user_id == user_id).first()
        if like:
            cu.like = 1
    user: User = User.query.get(user_id)
    # 查询关注数、被关注数、文章被点赞数
    user_like_count = Like.query.filter(Like.user_id == user_id, Like.target_user_id > 0).count()
    like_user_count = Like.query.filter(Like.target_user_id == user_id).count()
    post_all = Post.query.filter(Post.user_id == user_id).all()
    post_like_count = 0
    for post in post_all:
        post_like_count += post.like_count

    user.user_like_count = user_like_count
    user.like_user_count = like_user_count
    user.post_like_count = post_like_count
    return render_template(
        "user_profile.html",
        user=user,
        profile_type=0,
        cu=cu
    )


@page_profile.route("/user/discuss/<int:user_id>")
def user_discuss(user_id):
    pageSize = 5
    page = request.values.get('page')
    try:
        page: int = int(page)
    except Exception as v:
        print(v)
    if not page:
        page = 1
    user = User.query.get(user_id)
    post_all = Post.query.filter(Post.user_id == user_id).order_by(Post.create_time.desc()).paginate(page=page, per_page=pageSize)
    post_count = Post.query.filter(Post.user_id == user_id).count()
    if not user:
        return abort(404)
    user: User = User.query.get(user_id)
    for post in post_all:
        user = User.query.filter(User.id == post.user_id).first()
        post.username = user.username
        post.header_url = user.header_url
    return render_template(
        "user_discuss.html",
        user=user,
        post_all=post_all,
        post_count=post_count,
        profile_type=1,
        pagination=post_all
    )


@page_profile.route("/user/comment/<int:user_id>")
def user_comment(user_id):
    pageSize = 5
    page = request.values.get('page')
    try:
        page: int = int(page)
    except Exception as v:
        print(v)
    if not page:
        page = 1
    post_count = Post.query.filter(Post.user_id == user_id).count()
    comment_all = Comment.query.filter(Comment.user_id == user_id).paginate(page=page, per_page=pageSize)
    for comment in comment_all:
        post = Post.query.filter(Post.id == comment.target_post_id).first()
        comment.post_title =post.title
    comment_count = comment_all.total
    user: User = User.query.get(user_id)
    return render_template(
        "user_comment.html",
        user=user,
        post_count=post_count,
        comment_all=comment_all,
        comment_count=comment_count,
        profile_type=2,
        pagination=comment_all
    )


# 关注列表
@page_profile.route("/user/followees/<int:user_id>")
def user_followee(user_id: int):
    # 判断用户是否登录
    cu = User()
    cu.id = 0
    if current_user.is_authenticated:
        cu = current_user
    # 获取当前页面对应的用户
    user: User = User.query.get(user_id)
    user.cu_id = cu.id
    # 获取该页面用户的全部关注者
    user_like_all = Like.query.filter(Like.user_id == user_id, Like.target_user_id > 0).all()

    if current_user.is_authenticated:
        cu = current_user


    # 获取关注者的id
    followee_all = []
    for user_like in user_like_all:
        follower: User = User.query.get(user_like.target_user_id)
        follower.like_create_time = user_like.create_time
        like = Like.query.filter(Like.user_id == cu.id, Like.target_user_id == follower.id).first()
        if like:
            follower.like = 1
        followee_all.append(follower)
    return render_template(
        "user_followees.html",
        user=user,
        followee_all=followee_all
    )


# 粉丝列表
@page_profile.route("/user/followers/<int:user_id>")
def user_follower(user_id: int):
    cu = User()
    cu.id = 0
    user: User = User.query.get(user_id)
    like_user_all = Like.query.filter(Like.target_user_id == user_id).all()
    follower_all = []
    if current_user.is_authenticated:
        cu = current_user
    user.cu_id = cu.id
    for like_user in like_user_all:
        follower: User = User.query.get(like_user.user_id)
        follower.like_create_time = like_user.create_time
        like = Like.query.filter(Like.user_id == cu.id, Like.target_user_id == follower.id).first()
        if like:
            follower.like = 1
        follower_all.append(follower)
    return render_template(
        "user_followers.html",
        user=user,
        follower_all=follower_all,
    )
